import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.*;


public class UserProfilesDialog extends JFrame{
	
	private JLabel title;
    private JList list;
    private JButton selectButton;
    private JPanel row;
    private SearchResultsDialog searchResults;
    private Vector<UserPreferences> userPrefs;
    private DomainLogic dL;
    
    public UserProfilesDialog(Vector<UserPreferences> _userPrefs, DomainLogic _dL){
    	userPrefs = _userPrefs;
    	dL = _dL;
    	
    	title = new JLabel("Select a Profile");
    	this.add(title);
    	
    	row = new JPanel();
    	row.setLayout(new BoxLayout(row, BoxLayout.X_AXIS));
    	
		String[] profiles = new String[userPrefs.size()];
		for(int i = 0; i < userPrefs.size(); i++)
			profiles[i] = userPrefs.get(i).getName(); //add address
		
		this.setLayout(new GridLayout(0,1));
		//this.setMinimumSize(new Dimension(400,400));
		
		list = new JList(profiles);
		list.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
		list.setLayoutOrientation(JList.HORIZONTAL_WRAP);
		list.setVisibleRowCount(-1);
		JScrollPane listScroller = new JScrollPane(list);
		listScroller.setMaximumSize(new Dimension(200, 80));
		this.add(listScroller);

		selectButton = new JButton("Select");
		selectButton.addActionListener(new ActionListener(){
	        public void actionPerformed(ActionEvent e){
	        	UserPreferences selectedProfile = userPrefs.get(list.getSelectedIndex());
				String foodType = selectedProfile.getType();
				String zipCode = selectedProfile.getZipCode();				
				Preferences prefs = selectedProfile.getPreferences();
				Vector<Restaurant> v = dL.findRestaurants(foodType, zipCode,prefs);
				searchResults = new SearchResultsDialog(v, dL, true , false, foodType,zipCode,prefs, selectedProfile.getName());
				searchResults.setVisible(true);		
	            }
			});
		selectButton.setMinimumSize(new Dimension(100,30));
		selectButton.setMaximumSize(new Dimension(100,30));
		row.add(selectButton);
		this.add(row);
		this.setSize(300,200);
    }
}
